<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="搜索客户" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入外部联系人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跟进状态" prop="followUpStatus">
        <el-select v-model="queryParams.followUpStatus" placeholder="请选择跟进状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="企业标签" prop="tag">
        <el-select v-model="queryParams.tags" multiple placeholder="请选择标签">
          <el-option-group
            v-for="group in tagGroups"
            :key="group.id"
            :label="group.groupName">
            <el-option
              v-for="tag in group.tagList"
              :key="tag.id"
              :label="tag.name"
              :value="tag.id">
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item label="添加方式" prop="status">
        <el-select v-model="queryParams.addWay" placeholder="请选择添加方式" clearable size="small">
          <el-option
            v-for="dict in addwayOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间" prop="createAt">
        <el-date-picker clearable size="small"
                        v-model="queryParams.addTime"
                        type="daterange"
                        range-separator="至"
                        unlink-panels
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :picker-options="pickerOptions"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh-right"
          size="mini"
          @click="syncContactDefine"
          v-hasPermi="['wework:contact:add']"
        >同步</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contactList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="外部联系人编号" align="center" prop="id" v-if="false"/>
      <el-table-column label="名称" width="240">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="40" :src="scope.row.avatar">zzz</el-avatar>
          <span style="marginRight: 10px">{{ scope.row.name }}</span>
          <i v-if="scope.row.gender=='1'" class="el-icon-male"></i>
          <i v-if="scope.row.gender=='2'" class="el-icon-female"></i>

          <el-tag v-if="scope.row.type=='1'" size="mini" type="success">@微信</el-tag>
          <el-tag v-if="scope.row.type=='2'" size="mini" >@企业微信</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="跟进状态" align="center" prop="followUpStatus" />
      <el-table-column label="归属" align="left" width="240">
        <template slot-scope="scope">
          <div v-for="emp in scope.row.contactEmployeeList" :key="emp.id" class="text item">
            {{emp.employeeName}} -- {{addwayText(emp.addWay)}} -- {{emp.remark}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center">
        <template slot-scope="scope">
          <el-tag v-for="tag in scope.row.tags" :key="tag.id" class="text item">
            {{tag.name}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="企业简称" align="center" prop="corpName" />
      <el-table-column label="添加方式" align="center" width="120">
        <template slot-scope="scope">
          {{addwayText(scope.row.addWay)}}
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="addTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="120">
        <template slot-scope="scope">
          <div>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</div>
          <div>{{ parseTime(scope.row.addTime, '{h}:{i}:{s}') }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wework:contact:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listContact, getContact, delContact, addContact, updateContact, exportContact,syncContact } from "@/api/wework/contact";
import {listTag} from "@/api/wework/tag"
import FileUpload from '@/components/FileUpload';
import {syncEmployee} from "@/api/wework/employee";
import Template from "@/views/multitenant/template";

export default {
  name: "Contact",
  components: {
    Template,
    FileUpload,
  },
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      contactList: [],
      tagGroups:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        nickName: undefined,
        followUpStatus: undefined,
        type: undefined,
        corpName: undefined,
        corpFullName: undefined,
        externalProfile: undefined,
        businessNo: undefined,
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 表单参数
      form: {},
      // 表单校验
    };
  },
    created() {
      this.getList();
      this.getTagList();
      this.getDicts("contact_wx_addway").then(response => {
        this.addwayOptions = response.data;
      });
    },
    methods: {
      /** 查询客户管理列表 */
      getList() {
        this.loading = true;
        listContact(this.queryParams).then(response => {
          this.contactList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getTagList(){
        this.loading = true;
        listTag().then(resp=>{
          this.tagGroups = resp.data;
          this.loading = false;
        })
      },
      addwayText(addway){
        let text =  this.selectDictLabel(this.addwayOptions,addway)
        console.log(text)
        return text;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          corpId: undefined,
          wxExternalUserid: undefined,
          name: undefined,
          nickName: undefined,
          avatar: undefined,
          followUpStatus: 0,
          type: undefined,
          gender: undefined,
          unionid: undefined,
          position: undefined,
          corpName: undefined,
          corpFullName: undefined,
          externalProfile: undefined,
          businessNo: undefined,
          createTime: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      // 同步企业微信
      syncContactDefine () {
        syncContact().then(res => {
          // this.getTableData()
          this.$message.info('后台同步中,请稍等')
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 排序触发事件 */
      handleSortChange(column, prop, order) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
        this.getList();
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有客户管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportContact(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
